package club.cearnach.web.controller.admin;

import club.cearnach.core.util.StringUtil;
import club.cearnach.web.controller.BaseController;
import club.cearnach.api.dto.MovieDTO;
import club.cearnach.entity.Movie;
import club.cearnach.entity.MovieLanguage;
import club.cearnach.entity.MovieType;
import club.cearnach.api.exception.*;
import club.cearnach.api.form.PageBean;
import club.cearnach.api.service.*;
import club.cearnach.api.service.impl.MovieServiceImpl;
import club.cearnach.api.vo.admin.AdminMovieVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author 阮胜
 * @date 2018/5/13 16:54
 */
@Api(tags = "后台电影控制类")
@Controller
@RequestMapping("/admin/movie")
@Slf4j
public class BackgroundMovieController extends BaseController {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private CacheManager cacheManager;

    public BackgroundMovieController(IActorService actorService, IAdminService adminService, IDirectorService directorService, IMovieLanguageService movieLanguageService, IMovieService movieService, IMovieTypeService movieTypeService, IPersonService personService, IPlaceService placeService, IVisitorService visitorService) {
        super(actorService, adminService, directorService, movieLanguageService, movieService, movieTypeService, personService, placeService, visitorService);
    }

    @PostMapping("/save")
    @ResponseBody
    public boolean save(@Valid MovieDTO movieDTO) throws MovieLanguageException, MovieTypeException, MovieNotFoundException, IOException, MoviePathException {
        movieService.save(movieDTO);
        return true;
    }


    @GetMapping("/list")
    public String list(PageBean pageBean, Model model) {
        Page<Movie> moviePage = movieService.findAllOrderByCreateDateDesc(pageBean);
        List<Movie> movieList = moviePage.getContent();
        model.addAttribute(movieList);
        model.addAttribute("totalPage", moviePage.getTotalPages());
        model.addAttribute("currentPage", pageBean.getPage());
        return "admin/movie-list";
    }

    @GetMapping("/list/image")
    public String imageList(PageBean pageBean, Model model) {
        Page<Movie> moviePage = movieService.findAllOrderByRateDesc(pageBean);
        model.addAttribute(moviePage.getContent());
        model.addAttribute("totalPage", moviePage.getTotalPages());
        model.addAttribute("currentPage", pageBean.getPage());
        return "admin/movie-image-list";
    }



    @ApiOperation("跳转到编辑电影页面")
    @GetMapping("/form/save/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) throws MovieException {
        AdminMovieVO adminMovieVO = new AdminMovieVO();
        if (id != 0) {
            //由于使用了缓存,此时movie在缓存里面处于游离态,需要先删除缓存的movie,从数据库取得持久态的对象.
            cacheManager.getCache(MovieServiceImpl.CACHE_MOVIE_NAME).evict("movie_" + id);
            Movie movie = movieService.findById(id);
            BeanUtils.copyProperties(movie, adminMovieVO);
            String actorStr = StringUtil.entityCollection2String(movie.getActors(), "/");
            String directorStr = StringUtil.entityCollection2String(movie.getDirectors(), "/");
            adminMovieVO.setActors(actorStr);
            adminMovieVO.setDirectors(directorStr);
            adminMovieVO.setLanguageId(movie.getMovieLanguage().getId());
            adminMovieVO.setTypeId(movie.getType().getId());
            model.addAttribute(adminMovieVO);
        }
        List<MovieLanguage> movieLanguageList = movieLanguageService.findAll();
        List<MovieType> movieTypeList = movieTypeService.findAll();
        model.addAttribute(movieLanguageList);
        model.addAttribute(movieTypeList);
        return "admin/movie-save";
    }

    @ApiOperation("删除人物信息")
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable("id") Integer id) throws MovieException {
        movieService.delete(id);
        return true;
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/delete/all")
    @ResponseBody
    public long deleteAll(@RequestParam(value = "id") Integer[] idArr) {
        return movieService.deleteIn(idArr);
    }


}
